package com.android.presenter.info;

import android.content.Context;

import com.android.model.entity.res.CreateTokenResult;
import com.android.model.info.LocalSp;
import com.android.model.info.UserModel;
import com.android.model.net.HttpObserver;
import com.android.presenter.base.BasePresenter;
import com.android.presenter.iview.ICreateTokenView;
import com.android.util.log.LogUtil;

/**
 * author:  TenFace
 * date:    2017/9/27
 * description: 创建 Token，token 主要用于查询类操作
 */

public class CreateTokenPresenter extends BasePresenter<ICreateTokenView> {

    private static final String TAG = CreateTokenPresenter.class.getSimpleName();
    private UserModel mUserModel;

    public CreateTokenPresenter(ICreateTokenView iCreateTokenView) {
        super(iCreateTokenView);
        mUserModel = UserModel.getInstance();
    }

    public void createToken(final Context context,
                            String accountId,
                            String tokenType,
                            String accountToken) {

        mUserModel.createToken(accountId, tokenType, accountToken, new HttpObserver<CreateTokenResult>() {
            @Override
            public void onSuccess(CreateTokenResult createTokenResult) {
                LogUtil.e(TAG, "成功！");
                if (mIView != null) {
                    mIView.createTokenSuccess(createTokenResult);
                    LocalSp.getInstance(context).saveOrderTradeToken(createTokenResult.getValue());
                    LocalSp.getInstance(context).saveOrderTradeTokenExpiredTime(createTokenResult.getExpiredAt());
                }
            }

            @Override
            public void onError(int errType, String errMessage) {
                LogUtil.e(TAG, "失败码：" + errType + "》》信息：" + errMessage);
                if (mIView != null) {
                    mIView.createTokenFail(errType, errMessage);
                }
            }
        }, mIView.getLifeSubject());
    }

}
